import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
// import 'package:install_plugin/install_plugin.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

/**
 *Created by Jcy
 *on 2021/9/28
 */
class AppUpDate {
  void onClickInstallApk(String savePath) async {
    print(savePath);
    if (savePath.isEmpty) {
      print('make sure the apk file is set');
      return;
    }
    if (await Permission.storage.request().isGranted) {
      //打包android需要放开 打包ios需要注释
      // InstallPlugin.installApk(savePath, '包名').then((result) {
      //   print('install apk $result');
      // }).catchError((error) {
      //   print('install apk error: $error');
      // });
    }
  }

  ///文件下载进度
  static showDownloadProgress(received, total) {
    String _received = '';
    if (total != -1) {
      print((received / total * 100).toStringAsFixed(0) + "%");
      _received = (received / total * 100).toStringAsFixed(0) + "%";
      return _received;
    }
  }

  ///获取Android和IOS版本号
  ///Android和IOS都需要
  /// PackageInfo packageInfo = await PackageInfo.fromPlatform();
  ///String _versionName = "${packageInfo.version}";
  getVersion() async {
    try {
      Response response = await new Dio().post("下载地址");
      if (response.statusCode == 200) {
        print(response.data);
        Map<String, dynamic> map = json.decode(response.data);
      }
    } on DioError catch (e) {} finally {}
  }

  ///获取存储权限
  static Future<bool> checkPermission() async {
    if (Platform.isAndroid) {
      if (await Permission.storage.request().isGranted) {
        return true;
      }
    }
    return false;
  }

  static Future<String?> findLocalPath() async {
    final directory = Platform.isAndroid
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();

    return directory!.path;
  }
}
